/*
 * StringUtils.h
 *
 *  Created on: Nov 28, 2009
 *      Author: tptfc
 */

#ifndef STRINGUTILS_H_
#define STRINGUTILS_H_

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class StringUtils {
public:
	/*
	 *	@postcondition	stringToTrim not changed
	 *
	 *	@return trimmed string
	 */
	static string trim(const string strinToTrim) {
		size_t s = strinToTrim.find_first_not_of(" \n\r\t");
		size_t e = strinToTrim.find_last_not_of (" \n\r\t");

		if(( string::npos == s) || ( string::npos == e))
			return "";
		else
			return strinToTrim.substr(s, e-s+1);
	}

	/*
	 *	@postcondition	stringToSplit and textToFind not changed
	 */
	static vector<string> split(const string stringToSplit, const string textToFind) {
		vector<string> strings;

		string backText = stringToSplit;

		int index = backText.find(textToFind);

		while (index != -1) {
			strings.push_back(backText.substr(0, index));
			backText = backText.replace(0, index+1, "");
			index = backText.find(textToFind);
		}

		strings.push_back(backText);

		return strings;
	}

	/**
	 * @return	true if string is empty
	 */
	static bool isEmpty(string text) {
		return trim(text).empty();
	}

	static bool endWith(string text, string findText) {
		return text.rfind(findText) + findText.length() == text.length();
	}
};

#endif /* STRINGUTILS_H_ */
